



	var url_notes = "/admin/process/zTreeRole.html";//获取url 
	var url_applySave = "/admin/process/applySave.html";


	var listjson =  listjson || {};
	var process_id =  process_id || null;
	var step = 1;
	
	var $tbody = $('#process_tbody');
	
	var new_tr = '<tr class="text-c">\
                <td>\
                    第<span class="step_num"></span>步\
                </td>\
                <td class="text-l step_roles" ></td>\
                <td>\
                    <button type="button" class="btn btn-secondary radius size-MINI" onclick="process_step_upt(this)">\
                        <i class="Hui-iconfont"></i> 修改\
                    </button>\
                    &nbsp;\
                    <button type="button" class="btn btn-danger radius size-MINI" onclick="process_step_del(this)">\
                        <i class="Hui-iconfont"></i> 删除\
                    </button>\
                </td>\
            </tr>';
//设置 步骤数字 
function setStepNum(){
	// console.log('setStepNum:');
	// var i = 1;
	// $tbody.find('.step_num').each(function(){
	// 	$(this).text(i++);
	// });
		
}


//添加步骤
function process_add(){

	listjson.push([]);
	create();



}

//保存
function process_save(){
	console.log(listjson);

	//jBox.tip('loading...' , 'loading');
    $.ajax({
        type:'post',
        url:url_save,
        data: {process_id : process_id , steps: listjson },
        dataType:'json',
        success:function(d){
        	if(d.code == 1 ){
        		jBox.tip(d.msg, 'success');
        		setTimeout(function(){location.reload();},1000);
        	}else{
        		jBox.tip(d.msg, 'error');
        	}
        	

        },
        error:function(a,b,c){
            jBox.tip('ajax error：' + b );
        }
    });


}



//步骤修改
function process_step_upt(obj){
	console.log('process_step_upt:');
	var $tr = $(obj).closest("tr");
	var $div = $tr.find('[data-role]');
	$id = [];

	step = $tr.data('i') ;

	$.each($div , function(){
		$id.push( $(this).data('role'));
	});

	console.log($id);
	ztreeSelect({ids:$id,multi : true , type:1 ,ok:process_step_upt_callback})

}

function process_step_upt_callback(nodes){

	var nodes = nodes ||[];
	var arr = [];
	$.each(nodes,function(i,v){
		arr.push({ role_id: v.id, place: v.place , users_name: v.users_name });
		console.log(arr);
	});

	listjson[step] = arr;
	create();
}



//步骤删除
function process_step_del(obj){
	console.log('process_step_del:', obj );
	var $tr = $(obj).closest("tr");
	var step = $tr.data('i') ;

	listjson.splice(step,1);


	$(obj).closest("tr").css('background','#ddd').fadeOut(500,function(){  create();  });


}


function create(){
	var list = listjson || [] ;
	console.log(list);
	$tbody.empty();
	$.each(list , function(i,v){

		$tr = $(new_tr).data('i' , i );
		$tbody.append($tr);
		$tr.find('.step_num').text(i+1);

		$.each( v , function (ii,vv){
			var $div = $('<div data-role="'+vv.role_id+'">'+vv.place+' <br/> ----（'+ (vv.users_name) +'）</div>');
			$tr.find('.step_roles').append($div);
		});
	});

}












function init(){
	create();	
}
init();